package controller.Operation;

import model.NormalUser;
import util.BookDao;
import util.RecordDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.text.ParseException;

/**
 * @Question ReturnBook
 * @Date 2022/12/19 20:42
 * @Solution
 */
public class ReturnBook {
    public static void returnBook(NormalUser user , JTable bookTable, DefaultTableModel bookModel, DefaultTableModel recordModel, JTable recordTable) throws ParseException {
        //检查是否有行被选中
        int row = recordTable.getSelectedRow();
        if(row==-1) {
            JOptionPane.showMessageDialog(null, "请选择需要续借的书籍!");
            return;
        }
        //获取到该行记录的id
        int id=(int) recordModel.getValueAt(row,0);
        int bookId=(int)bookTable.getValueAt(row,0);
        //确认删除
        int opt = JOptionPane.showConfirmDialog(null,
                "请确认是否归还？", "确认归还",
                JOptionPane.YES_NO_OPTION);
        if (opt == JOptionPane.YES_OPTION) {
            //确认继续操作
            //删除
            RecordDao recordDao = new RecordDao();
            recordDao.deleteById(id);
        }
        BookDao bookDao = new BookDao();
        bookDao.reviseState(bookId,0);
        Show.showByUserIdRecord(recordModel, user.getId());
        Show.showAllBook(bookModel);
        JOptionPane.showMessageDialog(null, "归还成功！");
    }
}
